<?php
/**
 * Created by PhpStorm.
 * User: 沁塵
 * Date: 2019/4/20
 * Time: 19:57
 */

namespace app\api\controller\v1;

use app\api\model\ProductIntroduce as ProductIntroduceModel;
use app\api\model\ProductIntroduceZimu as ProductIntroduceZimuModel;
use think\Request;

class ProductIntroduce
{

    public function getProductText(Request $request)
    {
        $event = new ProductIntroduceModel();
        $list = $event->where("status", 1)->limit(10)->select()->toArray();
        $savedata = [];
        if (!empty($list)) {
            foreach ($list as $li) {
                $item =[];
                $item["id"] = $li["id"];
                $item["status"] = 2;
                $savedata[] = $item;
            }
            $res = $event->saveAll($savedata);
            return writeJson(200,$list,'完成！');
        } else {
            return writeJson(200,[],'完成！');
        }
    }

    public function backfill(Request $request)
    {
        $params = $request->post();
        $info = ProductIntroduceModel::where("id", $params["id"])->findOrEmpty();
        if (!$info->isEmpty()) {
            $info->status = $params["status"];
            $info->url = $params["url"];
            $info->duration = $params["duration"];
            $info->save();
            return writeJson(200,$info,'成功！');
        } else {
            return writeJson(400,[],'找不到！');
        }
    }


    public function getInfo(Request $request)
    {
        $params = $request->post();
        $info = ProductIntroduceModel::where("id", $params["id"])->findOrEmpty();
        if ($info->isEmpty()) {
            return writeJson(400, "", '没有');
        } else {
            return writeJson(200, $info["text"], '请求成功');
        }
    }

    public function getProductTextList(Request $request)
    {
        $params = $request->get();
        $info = ProductIntroduceModel::where("sound_id", $params["sound_id"])->select()->toArray();
        if (!empty($info)) {
            return writeJson(200,$info,'成功！');
        } else {
            return writeJson(200,[],'暂无数据！');
        }
    }

    public function getIntroduce(Request $request)
    {
        $params = $request->post();
        $list = ProductIntroduceModel::where("sound_id", $params["sound_id"])
            ->where("pid", $params["pid"])
            ->orderRand()->findOrEmpty();

        return writeJson(200,$list,'成功！');
    }

    public function getZimu(Request $request)
    {
        $params = $request->post();
        $info = ProductIntroduceZimuModel::where("pid", $params["pid"])
            ->where("text", $params["text"])
            ->findOrEmpty();

        return writeJson(200,$info,'成功！');
    }
}